<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1. 不同路径 - 用数论：就是排列组合的方式，C(8, 2) 从把个里面挑出2个数的可能性，8 * 7 / 2 * 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
        /* 
            时间复杂度O(m - 1) 就是for循环的step遍历
            空间复杂度O(1)
        */
        function uniquePaths2(m, n) {
            let res = 1
            let step = m - 1
            let total = m + n - 2
            // 
            for (let i = 1; i <= step; i++) {
                res = res * (total - i + 1) / i
            }
            return res
        }
        console.log(uniquePaths2(3, 7));
        console.log(uniquePaths2(5, 4));
    </script>
</body>

</html>